package com.lancoo.klgcourseware.ui.fragment.usage;

import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import android.view.View;

import com.lancoo.klgcourseware.base.BaseKlgFragment;
import com.lancoo.klgcourseware.entity.ExpressPracticeModel;
import com.lancoo.klgcourseware.ui.fragment.basicTrain.BasicTrainBaseFragment;
import com.stkouyu.entity.LgEvaluateObj;

/**
 * Created by pengmi on 2019/8/24 19:15
 * Description:
 */
public abstract class UsageSubBaseFragment extends BaseKlgFragment {

    protected enum UsageState{
        SENTENCERECORD,WORDRECORD,SENTENCERESULT,WORDRESULT,THREESECOND,REPEATMEDIAPLAY
    }

    private static final String TAG = "UsageSubBaseFragment";
    public static final String KEY_EXPRESSPRACTICE="expressPractice";
    public static final String KEY_QUESINDEX="quesIndex";
    public static final String KEY_QUESTOTAL="quesTotal";
    public static final String KEY_ALERT="alert";
    protected final int QUALITYSCORE=60;
    protected ExpressPracticeModel expressPracticeModel;
    protected int quesIndex,quesTotal;
    protected UsageState currentUsageState=UsageState.THREESECOND;
    protected boolean isQuality;

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        expressPracticeModel=getArguments().getParcelable(KEY_EXPRESSPRACTICE);
        quesIndex=getArguments().getInt(KEY_QUESINDEX);
        quesTotal=getArguments().getInt(KEY_QUESTOTAL);
        super.onViewCreated(view, savedInstanceState);
    }

    /**
     * 开始句子朗读作答
     */
    public void startSentenceRecord(){
        startRecordTrain();
    }

    /**
     * 语音评测回调
     * @param grade
     * @param url
     * @param skegnResult
     */
    public void speechEvaluationCallBack(int grade, String url, LgEvaluateObj skegnResult){
        onSuccessSpeechEvaluation(grade,url,skegnResult);
    }

    public void recordProgressUpDate(int progress){
        upDateRecordProgress(progress);
    }

    public void upDateSecondTimer(int second){
        upDateSecondCountime(second);
    }

    public void upDateSecondTimerProgress(int progress,int totalProgress){
        updateResultCountTime(progress,totalProgress);
    }

    public int getSubUsaeTrainScore(){
        return getScore();
    }

    public void pauseTrain(boolean isReset){
        pauseSubTrain(isReset);
    }

    public boolean getIsQuality(){
        return isQuality;
    }

    /**
     * 恢复训练
     */
    public void resumeTrain(){
        continueTrain();
    }

    public void updateCountTimeProgress(int progress,int totalProgress){
        updateCountTime(progress,totalProgress);
    }

    public void updateMediaPlayProgress(int progress){
        updateMediaPlayerProgress(progress);
    }

    public void imgAninationStateChange(boolean isPlaying){
        changeImageViewAnimationState(isPlaying);
    }

    public void clearSubUsageTrainResult(){
        clearAnswerResult();
    }


    /****************内部调用*********************/
    protected void startRecordTrain(){}

    protected void onSuccessSpeechEvaluation(int grade, String url, LgEvaluateObj skegnResult){}

    protected void upDateRecordProgress(int progress){}

    protected void upDateSecondCountime(int second){}

    protected int getScore(){
        return 0;
    }

    protected void pauseSubTrain(boolean isReset){}

    protected void continueTrain(){}

    protected void changeImageViewAnimationState(boolean isPlaying){}

    protected void clearAnswerResult(){}

    protected void updateCountTime(int progress,int totalProgress){}

    protected void updateResultCountTime(int progress,int totalProgress){}

    protected void updateMediaPlayerProgress(int progress){}


    protected void startSpeechEvaluation(String url,String exaluationSentence){
        if(getParentFragment() instanceof BasicTrainBaseFragment){
            ((BasicTrainBaseFragment)getParentFragment()).startRecordSpeechEvaluation(url,exaluationSentence);
        }
    }

    protected void stopSpeechEvaluation(){
        if(getParentFragment() instanceof BasicTrainBaseFragment){
            ((BasicTrainBaseFragment)getParentFragment()).pauseTimer();
            ((BasicTrainBaseFragment)getParentFragment()).stopRecordSpeechEvaluation();
        }
    }

    protected void startNextTrainCountTime(){
        if(getParentFragment() instanceof BasicTrainBaseFragment){
            ((BasicTrainBaseFragment)getParentFragment()).startSecondCountTime();
        }
    }

    protected void startNextQuesTraion(){
        if(getParentFragment() instanceof BasicTrainBaseFragment){
            ((BasicTrainBaseFragment)getParentFragment()).startNextQuesTraion();
        }
    }

    protected void startDripMusic(){
        if(getParentFragment() instanceof BasicTrainBaseFragment){
            ((BasicTrainBaseFragment)getParentFragment()).startDripMusic();
        }
    }

    protected void playResultMusic(boolean isCorrect){
        if(getParentFragment() instanceof BasicTrainBaseFragment){
            ((BasicTrainBaseFragment)getParentFragment()).usageQuesTrainResultMediaPlayer(isCorrect);
        }
    }
    protected void playDemonstrationMusic(){
        if(getParentFragment() instanceof BasicTrainBaseFragment){
            ((BasicTrainBaseFragment)getParentFragment()).playDemonstrationSound(0);
        }
    }


    protected void pauseDemonstrationMusic(){
        if(getParentFragment() instanceof BasicTrainBaseFragment){
            ((BasicTrainBaseFragment)getParentFragment()).pauseTrainStudy(false);
        }
    }

    protected void restartCountTimeTrain(){
        if(getParentFragment() instanceof BasicTrainBaseFragment){
            ((BasicTrainBaseFragment)getParentFragment()).restartCountTimeTrain();
        }
    }

    protected void startTrainCountTime(){
        if(getParentFragment() instanceof BasicTrainBaseFragment){
            ((BasicTrainBaseFragment)getParentFragment()).startTrainDialogCountTime(0);
        }
    }


}
